package com.chinatsp.onlineapp.protocol;

import android.content.Context;

import com.chinatsp.onlineapp.base.BaseApplication;
import com.chinatsp.onlineapp.base.Config;
import com.chinatsp.onlineapp.presenter.ActiveDeviceDao;
import com.chinatsp.onlineapp.presenter.HostDao;
import com.chinatsp.onlineapp.presenter.ReplaceTokenDao;
import com.chinatsp.onlineapp.utils.TimeTool;

import tsp.cn.http.interfac.IProtolResult;
import tsp.cn.http.interfac.INetResult;
import tsp.cn.http.tools.AppUtil;
import tsp.cn.http.tools.LogUtil;
import tsp.cn.http.tools.PreferenceUtils;

public class ProtocalTools implements INetResult{

	
	static boolean mActiveSuccess = false;
	IProtolResult mIProtolResult;
	HostDao mHostDao;
	ActiveDeviceDao mActiveDeviceDao;
	ReplaceTokenDao mReplaceTokenDao;
	Context mContext;
	
	public ProtocalTools(Context c){
		mContext = c;
		mActiveSuccess = PreferenceUtils.getPreferenceBoolean("active_success", false);
	}
	
	public void startCommunicate(){
		chekHostParams();
	}
    
    public void setIProtolResult(IProtolResult l){
    	mIProtolResult = l;
    }
    public boolean isActiveSuccess(){ 
    	return mActiveSuccess;
    }
    
    public void requsetHostParams(){
    	mHostDao = new HostDao(mContext, this);
    	mHostDao.getHostParams();
    }
    
    public void chekHostParams(){
		if(needToUpdateHost()){ 
			LogUtil.d( "need update host"); 
			requsetHostParams();
		}else{
			LogUtil.d( "not need update host"); 
			checkActiveDecive();
		}
    }

    public boolean needToUpdateToken(){
    	String token_update_latest = PreferenceUtils.getPreference("token_update_latest");
    	
    	if(token_update_latest!=null&&token_update_latest!=""){

        	String current = TimeTool.getCurrentDates();
        	if(token_update_latest.equals(current)){
        		return false;
        	}else{
        		return true; 
        	}

    	}else{
    		return true;
    	}
    }
    
    public boolean needToUpdateHost(){
    	String host_latest = PreferenceUtils.getPreference("host_latest");
    	String ucs_api_host = PreferenceUtils.getPreference("ucs_api_host");
    	
    	if(host_latest!=null&&host_latest!=""&&ucs_api_host!=null&&ucs_api_host!=""){

        	String current = TimeTool.getCurrentDates();
        	if(host_latest.equals(current)){
        		return false;
        	}else{
        		return true; 
        	}

    	}else{
    		return true;
    	}
    }
    

    void checkUpdateToken(){
		//if(needToUpdateToken()){
		if(false){
			LogUtil.d( "need To Update Token");
			
			String current_date = TimeTool.getCurrentDates();
			PreferenceUtils.savePreference("token_update_latest", current_date);
			mReplaceTokenDao = new ReplaceTokenDao(mContext, ProtocalTools.this);
			mReplaceTokenDao.postReplace();
			
		}else{
			LogUtil.d( "not need ToUpdate Token"); 
			if(mIProtolResult!=null){
				mIProtolResult.onProtolResultSuccess();
			}
		}
    }
    void checkActiveDecive(){
		if(isActiveSuccess()){			
			LogUtil.d( "not need active sn"); 
			checkUpdateToken();
		}else{
			LogUtil.d( "need active sn"); 
			mActiveDeviceDao = new ActiveDeviceDao(mContext, this);
			mActiveDeviceDao.postActive();
		}
    }
	@Override
	public void onRequestSuccess(int requestCode) {
		// TODO Auto-generated method stub
		switch (requestCode) {
			case Config.CODE_GET_HOST_PARAMS:
				if(mHostDao.getHostParamsBean().getResp_status().equals("OK")){
					
					LogUtil.d( "get host ok");
					
					String factory_code = mHostDao.getHostParamsBean().getHostParamsDatas().getFactory_code();
					String ucs_api_host = mHostDao.getHostParamsBean().getHostParamsDatas().getUcs_api_host();
					
					LogUtil.d( "factory_code:"+factory_code); 
					LogUtil.d( "ucs_api_host:"+ucs_api_host); 
					
					if(factory_code!=null&&ucs_api_host!=null&&factory_code!=""&&ucs_api_host!=""){
						PreferenceUtils.savePreference("factory_code", factory_code);
						PreferenceUtils.savePreference("ucs_api_host", ucs_api_host);
						
						String current_date = TimeTool.getCurrentDates();
						PreferenceUtils.savePreference("host_latest", current_date);
					}
					
					checkActiveDecive();
					
				}else{
					LogUtil.d( "CODE_GET_HOST_PARAMS error"); 
				}
				break;
			case Config.CODE_GET_HOST_ACTIVE:{
					if(mActiveDeviceDao.getActiveBean().getResp_status().equals("OK")){
						
						LogUtil.d( "activiv sn ok");
						
						String tuid_sv = mActiveDeviceDao.getActiveBean().getResp_data().getTuid();
						LogUtil.d( "tuid_sv:"+tuid_sv); 
						String access_token_sv =  mActiveDeviceDao.getActiveBean().getResp_data().getAccess_token();
						
						LogUtil.d( "access_token_sv:"+access_token_sv); 
						if(tuid_sv!=null&&access_token_sv!=null&&tuid_sv!=""&&access_token_sv!=""){
							PreferenceUtils.savePreference("tuid_sv", tuid_sv);
							PreferenceUtils.savePreference("access_token_sv", access_token_sv);
							
							String current_date = TimeTool.getCurrentDates();
							PreferenceUtils.savePreference("token_update_latest", current_date);
						}
						
						
						mActiveSuccess = true;
						PreferenceUtils.setPreferenceBoolean("active_success", true);
											
						
						//if(mIProtolResult!=null){
							//mIProtolResult.onActiveResultSuccess();
						//}
						checkUpdateToken();
						
					}else{
						//设备已经激活的情况
						//if(mIProtolResult!=null){
							//mIProtolResult.onActiveResultSuccess();
						//}
						
						LogUtil.d( "CODE_GET_HOST_ACTIVE error"); 
						//checkUpdateToken();
						
					}
				}
				break;
			case Config.CODE_GET_REPLACE_TOKEN:{
				if(mReplaceTokenDao.getReplaceTokenBean().getResp_status().equals("OK")){
					
					String tuid_sv = mActiveDeviceDao.getActiveBean().getResp_data().getTuid();
					LogUtil.d( "uppdate tuid_sv"+tuid_sv); 
					String access_token_sv =  mActiveDeviceDao.getActiveBean().getResp_data().getAccess_token();
					
					LogUtil.d( "uppdate access_token_sv:"+access_token_sv); 
					if(tuid_sv!=null&&access_token_sv!=null&&tuid_sv!=""&&access_token_sv!=""){
						PreferenceUtils.savePreference("tuid_sv", tuid_sv);
						PreferenceUtils.savePreference("access_token_sv", access_token_sv);
					}
					
					if(mIProtolResult!=null){
						mIProtolResult.onProtolResultSuccess();
					}
					
				}else{
					LogUtil.d( "CODE_GET_REPLACE_TOKEN error"); 
					//if(mIProtolResult!=null){
					//	mIProtolResult.onActiveResultSuccess();
					//}
				}
			}
			break;
			default:
				break;
		}
	}
	@Override
	public void onRequestFailed(String errorNo, String errorMessage) {
		// TODO Auto-generated method stub
		/*
		switch (requestCode) {
		case Config.CODE_GET_HOST_PARAMS:{
			
			}
			break;
		case Config.CODE_GET_HOST_ACTIVE:{

			}
			break;
		case Config.CODE_GET_REPLACE_TOKEN:{

		}
		break;
		default:
			break;
	}*/
	}
	@Override
	public void onFinished() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onNoConnect() {
		// TODO Auto-generated method stub
		
	}
	
}
